//程序替换

#include<sys/types.h>
#include<sys/wait.h>
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<errno.h>
#include<stdlib.h>

//char *newenv = (char*) "MYVAL=6666666666";

char *const addenv[] = {
    (char*const)"MYVAL=123456789",
    NULL 
};

int main()
{
    printf("我的程序要运行了！\n");
    
    if(fork()==0)
    {
        printf("I am Child, My Pid Is: %d\n", getpid());
        sleep(1);

        char *const argv[] = {
            (char*const)"other",
            (char*const)"-a",
            (char*const)"-b",
            (char*const)"-c",
            (char*const)"-d",
            NULL 
        };

        //char *const env[] = {
        //    (char*const)"MYVAL=123456789",
        //    NULL 
        //};

        for(int i = 0; addenv[i]; i++)
        {
            putenv(addenv[i]);
        }
        extern char **environ;
        execvpe("./other", argv, environ);
        
        //putenv(newenv);

        //execvp("./other", argv);
        //execv("/usr/bin/ls", argv);

        //child
        //execl("/usr/bin/ls", "ls" ,"-ln", "-a", NULL);
        //execl("./other", "other", NULL);
        exit(1);
    }
    waitpid(-1, NULL, 0);
    printf("我的程序运行完毕了！\n");
    return 0;
}


